//
//  MOForgotPasswordViewCell.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/6/8.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOForgotPasswordViewCell.h"

@interface  MOForgotPasswordViewCell ()<UITextFieldDelegate>

@property(nonatomic,strong)UILabel *nameLabel;

//获取验证码
@property(nonatomic,strong)UIButton *getCodeButton;
//分割线
@property(nonatomic,strong)UIView *lineView;
//错误提示
@property(nonatomic,strong)UILabel *fieldLabel;

@end


@implementation MOForgotPasswordViewCell

+(instancetype)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath
{
    NSString *ID = @"MOForgotPasswordViewCell";
    MOForgotPasswordViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil){
        cell = [[MOForgotPasswordViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell creatView];
    }
    return cell;
}


-(void)creatView
{
    [self addSubview:self.nameLabel];
    [self addSubview:self.textField];
    [self addSubview:self.getCodeButton];
    [self addSubview:self.lineView];
    [self addSubview:self.fieldLabel];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"%@",textField.text);
    return YES;
}

-(void)clickCodeButton:(UIButton *)sender
{
    [sender SG_countdownWithSecond:60];
    if (self.clickCodeButtonBlock) {
        self.clickCodeButtonBlock();
    }
}

-(void)setModel:(MOForgotPasswordModel *)model
{
    if ([model.type integerValue]==1) {//获取验证码
        self.textField.width = SCREEN_WIDTH - 70 - 90;
        self.getCodeButton.hidden = NO;
        
    }else{
        self.textField.width = SCREEN_WIDTH - 70;
        self.getCodeButton.hidden = YES;
        
    }
    
    if ([model.type integerValue]==1 || [model.nameStr rangeOfString:@"手机号"].location!=NSNotFound) {
        self.textField.keyboardType = UIKeyboardTypeNumberPad;
    }else{
        self.textField.keyboardType = UIKeyboardTypeDefault;
    }
    
    self.nameLabel.text = model.nameStr;
    self.textField.placeholder = model.placeStr;
    if ([model.secureKeyword integerValue]==1) {
        _textField.secureTextEntry = YES;
    }else{
        _textField.secureTextEntry = NO;
    }
    if ([model.showField integerValue]==1) {
        self.fieldLabel.hidden = NO;
        self.lineView.backgroundColor = KRedColor_E71020;
    }else{
        self.fieldLabel.hidden = YES;
        self.lineView.backgroundColor = KGrayColor_ededed;
    }
    self.fieldLabel.text = model.fieldNameStr;
    
    self.textField.text = model.valueStr;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (self.endEditingBlock) {
        self.endEditingBlock(textField.text);
    }
}

#pragma mark -- 懒加载
-(UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(35, kHConversions(12), SCREEN_WIDTH-70, 12)];
        _nameLabel.font = FONT_SIZE(12);
        _nameLabel.textColor = KBlackColor_2A2A2A;
    }
    return _nameLabel;
}

-(UITextField *)textField
{
    if (!_textField) {
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(35, _nameLabel.bottom+kHConversions(12), SCREEN_WIDTH-70, 15)];
        _textField.delegate = self;
        _textField.textColor = KBlackColor_2A2A2A;
        _textField.clearButtonMode = UITextFieldViewModeAlways;
        _textField.font = FONT_SIZE(15);
    }
    return _textField;
}

-(UIButton *)getCodeButton
{
    if (!_getCodeButton) {
        _getCodeButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 35 - 90, _nameLabel.bottom+kHConversions(12), 90, 15)];
        _getCodeButton.titleLabel.font = FONT_SIZE(12);
        [_getCodeButton setTitle:@"获取短信验证码" forState:UIControlStateNormal];
        [_getCodeButton setTitleColor:KBlueColor_2C94FF forState:UIControlStateNormal];
        [_getCodeButton addTarget:self action:@selector(clickCodeButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _getCodeButton;
}

-(UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(35, self.textField.bottom + kHConversions(12), SCREEN_WIDTH-70, 1)];
        _lineView.backgroundColor = KGrayColor_ededed;
    }
    return _lineView;
}

-(UILabel *)fieldLabel
{
    if (!_fieldLabel) {
        _fieldLabel = [[UILabel alloc]initWithFrame:CGRectMake(35, self.lineView.bottom + kHConversions(12), SCREEN_WIDTH-70, 12)];
        _fieldLabel.textColor = KRedColor_FF4040;
        _fieldLabel.font = FONT_SIZE(12);
    }
    return _fieldLabel;
}


@end
